/*
   gcc Hello_Triangle.c  -L. -lGLESv2 -lm -lSDL2_ttf -lSDL2_test -lSDL2 && ./a.out &&
   gcc Hello_Triangle.c  -L. -lGLESv2 -lmingw32 -lSDL2_test -lSDL2main -lSDL2 -lSDL2_ttf && a 
gcc Hello_Triangle.c -L. -lGLESv2 -lEGL -lesUtil -lesUtil_d
 */
#include "SDL2/SDL.h"
#include "gles2base.h"

int Init ( UserData *userData)
{
   GLbyte vShaderStr[] =  
      "attribute vec4 vPosition;    \n"
      "void main()                  \n"
      "{                            \n"
      "   gl_Position = vPosition;  \n"
      "}                            \n";
   
   GLbyte fShaderStr[] =  
      "precision mediump float;\n"\
      "void main()                                  \n"
      "{                                            \n"
      "  gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
      "}                                            \n";

   GLuint vertexShader;
   GLuint fragmentShader;
   GLuint programObject;
   GLint linked;

   // Load the vertex/fragment shaders
   vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );
   fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );

   // Create the program object
   programObject = ctx.glCreateProgram ( );
   
   if ( programObject == 0 )
      return 0;

   ctx.glAttachShader ( programObject, vertexShader );
   ctx.glAttachShader ( programObject, fragmentShader );

   // Bind vPosition to attribute 0   
   ctx.glBindAttribLocation ( programObject, 0, "vPosition" );

   // Link the program
   ctx.glLinkProgram ( programObject );

   // Check the link status
   ctx.glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );

   if ( !linked ) 
   {
      GLint infoLen = 0;

      ctx.glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
      
      if ( infoLen > 1 )
      {
         char* infoLog = malloc (sizeof(char) * infoLen );

         ctx.glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
         SDL_Log( "Error linking program:\n%s\n", infoLog );            
         
         free ( infoLog );
      }

      ctx.glDeleteProgram ( programObject );
      return SDL_FALSE;
   }

   // Store the program object
   userData->programObject = programObject;

   ctx.glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
   return SDL_TRUE;
}

///
// Draw a triangle using the shader pair created in Init()
//
void Draw (UserData *userData)
{
   GLfloat vVertices[] = {  0.0f,  0.5f, 0.0f, 
                           -0.5f, -0.5f, 0.0f,
                            0.5f, -0.5f, 0.0f };
      
   // Set the viewport
   ctx.glViewport ( 0, 0, state->window_w, state->window_h);
   
   // Clear the color buffer
   ctx.glClear ( GL_COLOR_BUFFER_BIT );

   // Use the program object
   ctx.glUseProgram ( userData->programObject );

   // Load the vertex data
   ctx.glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
   ctx.glEnableVertexAttribArray ( 0 );

   /*ctx.glDrawArrays ( GL_TRIANGLES, 0, 3 );*/
   ctx.glDrawArrays ( GL_LINE_LOOP, 0, 3 );

   /*eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );*/
}

